package edu.yzu.testspringboot002.convert;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

import edu.yzu.testspringboot002.po.FileVO;
import edu.yzu.testspringboot002.po.Patient;
import edu.yzu.testspringboot002.po.dto.PatientInfoDTO;
import edu.yzu.testspringboot002.po.vo.PatinetVO;
import jodd.util.StringPool;
import org.mapstruct.Mapper;
import org.mapstruct.NullValuePropertyMappingStrategy;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 患者转换映射器
 */
@Mapper(
        componentModel = "spring",
        nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE
)
public abstract class PatientConvertMapper {


    public abstract Patient toPatientPO(PatientInfoDTO patientInfoDTO);


    public abstract PatientInfoDTO toPatientInfoDTO(Patient patient);

    public Patient toBuilderPatientPO(PatientInfoDTO patientInfoDTO) {
        //todo xj
        return null;
    }


    public List<PatientInfoDTO> toBuilderPatientInfoDTO(List<Patient> list) {
        List<PatientInfoDTO> patientInfoDTOList = new ArrayList<>();
        if (CollUtil.isEmpty(list)) {
            return patientInfoDTOList;
        }

        for (Patient patient : list) {
            PatientInfoDTO patientInfoDTO = this.toPatientInfoDTO(patient);
            Integer gender = patientInfoDTO.getGender();

            if(gender == 0){
                patientInfoDTO.setHeadLogo("https://prdservice.com/minio/edwin/chatHospitor/icon-user-04.png");
                patientInfoDTO.setIconLogo("https://prdservice.com/minio/edwin/chatHospitor/icon-women.png");
            }else {
                patientInfoDTO.setHeadLogo("https://prdservice.com/minio/edwin/chatHospitor/icon-user-05.png");
                patientInfoDTO.setIconLogo("https://prdservice.com/minio/edwin/chatHospitor/icon-men.png");
            }

            String genderStr = gender == 0 ? "女" : "男";
            patientInfoDTO.setPatientNameStr(patientInfoDTO.getPatientName() + " ( " + genderStr + " )");
            //获取诊断
            //patientInfoDTO.get
            patientInfoDTO.setPatientIdentityStr(String.format("诊断： %s, 年龄: %d", patientInfoDTO.getDisease() == null ? "未知" :  patientInfoDTO.getDisease(), patient.getAge()));
            String baseImage = patientInfoDTO.getBaseImage();
            String shadowImage = patientInfoDTO.getShadowImage();
            String shadowVideo = patientInfoDTO.getShadowVideo();
            String inspectionReport = patientInfoDTO.getInspectionReport();

            if (StrUtil.isNotBlank(baseImage)) {
                String[] split = baseImage.split(StringPool.COMMA);
                List<FileVO> fileVOList = new ArrayList<>();
                for (String str : split) {
                    String name = FileUtil.getName(str);
                    fileVOList.add(new FileVO(str, FileUtil.extName(name), name));
                }
                patientInfoDTO.setBaseImageList(fileVOList);
            } else {
                patientInfoDTO.setBaseImageList(new ArrayList<>());
            }

            if (StrUtil.isNotBlank(shadowImage)) {
                String[] split = shadowImage.split(StringPool.COMMA);
                List<FileVO> fileVOList = new ArrayList<>();
                for (String str : split) {
                    String name = FileUtil.getName(str);
                    fileVOList.add(new FileVO(str, FileUtil.extName(name), name));
                }
                patientInfoDTO.setShadowImageList(fileVOList);
            } else {
                patientInfoDTO.setShadowImageList(new ArrayList<>());
            }


            if (StrUtil.isNotBlank(shadowVideo)) {
                String[] split = shadowVideo.split(StringPool.COMMA);
                List<FileVO> fileVOList = new ArrayList<>();
                for (String str : split) {
                    String name = FileUtil.getName(str);
                    fileVOList.add(new FileVO(str, FileUtil.extName(name), name));
                }
                patientInfoDTO.setShadowVideoList(fileVOList);
            } else {
                patientInfoDTO.setShadowVideoList(new ArrayList<>());
            }

            if (StrUtil.isNotBlank(inspectionReport)) {
                String[] split = inspectionReport.split(StringPool.COMMA);
                List<FileVO> fileVOList = new ArrayList<>();
                for (String str : split) {
                    String name = FileUtil.getName(str);
                    fileVOList.add(new FileVO(str, FileUtil.extName(name), name));
                }
                patientInfoDTO.setInspectionReportList(fileVOList);
            } else {
                patientInfoDTO.setInspectionReportList(new ArrayList<>());
            }
            patientInfoDTOList.add(patientInfoDTO);
        }

        List<PatientInfoDTO> infoDTOList = patientInfoDTOList.stream().sorted(Comparator.comparing(PatientInfoDTO::getCreateTime)).collect(Collectors.toList());
        return CollUtil.reverse(infoDTOList);
    }

    /**
     * 更新患者po
     *
     * @param patientInfoDTO 患者信息dto
     * @param patient        病人
     * @return {@link Patient}
     */
    public Patient toUpdatePatientPO(PatientInfoDTO patientInfoDTO, Patient patient) {
        patient.setDisease(patientInfoDTO.getDisease());
        patient.setIntroduction(patientInfoDTO.getIntroduction());
        patient.setUpdateTime(new Date());
        builderFile(patientInfoDTO, patient);
        return patient;
    }

    /**
     * 生成器文件
     *
     * @param patientInfoDTO 患者信息dto
     * @param patient        病人
     */
    private static void builderFile(PatientInfoDTO patientInfoDTO, Patient patient) {
        if (CollUtil.isNotEmpty(patientInfoDTO.getBaseImageList())) {
            patient.setBaseImage(patientInfoDTO.getBaseImageList().stream().map(FileVO::getUrl)
                    .collect(Collectors.joining(StringPool.COMMA)));
        }

        if (CollUtil.isNotEmpty(patientInfoDTO.getInspectionReportList())) {
            patient.setInspectionReport(patientInfoDTO.getInspectionReportList().stream().map(FileVO::getUrl)
                    .collect(Collectors.joining(StringPool.COMMA)));
        }

        if (CollUtil.isNotEmpty(patientInfoDTO.getShadowImageList())) {
            patient.setShadowImage(patientInfoDTO.getShadowImageList().stream().map(FileVO::getUrl)
                    .collect(Collectors.joining(StringPool.COMMA)));
        }

        if (CollUtil.isNotEmpty(patientInfoDTO.getShadowVideoList())) {
            patient.setShadowVideo(patientInfoDTO.getShadowVideoList().stream().map(FileVO::getUrl)
                    .collect(Collectors.joining(StringPool.COMMA)));
        }
    }


    public abstract List<PatinetVO> toPatientVOList(List<Patient> patientList);


    public abstract PatinetVO toPatientVO(Patient patient);
}
